package com.example.gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * author: hyacinth
 * Date: 2023/5/25
 */
@Component
public class OnlineUserManager {
    // 保存用户在游戏大厅中的游戏状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();

    // 当前用户在当前游戏房间的游戏状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom  = new ConcurrentHashMap<>();
    /**
     * 玩家上线
     * @param userId
     * @param webSocketSession
     */
    public void enterGameHall(int userId, WebSocketSession webSocketSession){
        gameHall.put(userId,webSocketSession);
    }

    /**
     * 玩家下线
     * @param userId
     */
    public void exitGameHall(int userId){
        gameHall.remove(userId);
    }

    /**
     * 查询玩家状态
     * @param userId
     * @return
     */
    public WebSocketSession getSessionFromGameHall(int userId){
        return gameHall.get(userId);
    }

    // 当前用户在当前游戏房间的游戏状态
    public void enterRoom(int userId,WebSocketSession webSocketSession){
        gameRoom.put(userId,webSocketSession);
    }

    public void exitRoom(int userId){
        gameRoom.remove(userId);
    }

    public WebSocketSession getSessionFromGameRoom(int userId){
        return gameRoom.get(userId);
    }
}
